package com.syqc.monitor.ui.sms

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Build
import androidx.activity.viewModels
import autodispose2.autoDispose
import com.syqc.choose.car.ChooseCarActivity
import com.syqc.choose.teamcar.TeamsCarsActivity
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.ActivityResultUtil.launchAct
import com.syqc.comlib.utils.ActivityResultUtil.registerResultOk
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.toast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivitySmsSendBinding
import com.syqc.monitor.databinding.IncludeChooseCarBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.Keys
import com.syqc.utils.NetDataUtil


/**
 *@Author Jasper
 *@Time   2021/1/12
 *@Desc   发送指令
 */
class SMSSendActivity : BaseBarActivity<ActivitySmsSendBinding>(ActivitySmsSendBinding::inflate) {
    private lateinit var mSureVB: IncludeSelectSureBinding
    private lateinit var mChooseVB: IncludeChooseCarBinding
    private val mViewModel by viewModels<SMSSendViewModel>()

    override fun initView() {
        mSureVB = IncludeSelectSureBinding.bind(binding.root)
        mChooseVB = IncludeChooseCarBinding.bind(binding.root)
        ToolbarHelper.setToolbar(this, R.string.title_other_sms_edit)
        val desc = intent.getStringExtra(Keys.INTENT_OTHER) ?: ""
        val tmnBus = intent.getStringExtra(Keys.INTENT_TYPE) ?: ""
        val massage = intent.getStringExtra(Keys.INTENT_DATA) ?: ""

        val receiver = SMSReceiver()
        val intentFilter = IntentFilter()
        intentFilter.addAction("SMS_SEND_ACTION")

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
            registerReceiver(receiver, intentFilter, RECEIVER_NOT_EXPORTED)
        else {
            registerReceiver(receiver, intentFilter)
        }

        mSureVB.tvSure.text = getString(R.string.desc_cmd_send)
        binding.tvSmsDesc.text = desc
        binding.tvSmsTmnBus.text = tmnBus

        mViewModel.sendMsg = massage
        binding.editSmsContent.setText(massage)
        binding.editSmsContent.setSelection(massage.length)

        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mViewModel.simObserver().autoDispose(scopeProvider).subscribe {
            binding.editSmsSim.setText(it)
        }

    }

    override fun setListener() {
        binding.editSmsContent.doAfterChanged {
            mViewModel.sendMsg = it
        }
        binding.editSmsSim.doAfterChanged {
            mViewModel.simNum = it
        }

        mChooseVB.lrvChooseCar.filterFast {

            if (NetDataUtil.isSyncTree) {
                launcher.launchAct<TeamsCarsActivity>(this)
            } else {
                launcher.launchAct<ChooseCarActivity>(this)
            }
        }

        mSureVB.tvSure.filterFast {
            if (mViewModel.isParamsOk()) return@filterFast
            sendMsg()
        }
    }

    /**
     * 跳转到短信
     */
    private fun sendMsg() {
        val uri: Uri = Uri.parse("smsto:" + mViewModel.simNum)
        val intent = Intent()
        intent.action = Intent.ACTION_SENDTO
        intent.putExtra("sms_body", mViewModel.sendMsg)
        intent.type = "vnd.android-dir/mms-sms" //设置 MIME 类型

        intent.data = uri
        startActivity(intent)
    }

    private val launcher = registerResultOk { result ->
        result.data?.let {
            val carId = it.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
            val carName = it.getStringExtra(Keys.INTENT_CAR_NAME) ?: ""
            mChooseVB.lrvChooseCar.setRightText(carName)
            mViewModel.carId = carId
            binding.editSmsSim.setText("")
            mViewModel.getSimByCarId()
        }
    }

    inner class SMSReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) = when (resultCode) {
            RESULT_OK -> toast(R.string.toast_send_success)
            else -> toast(R.string.toast_send_faile)
        }
    }
}